package com.kgc.smbms.controller;

import com.github.pagehelper.PageInfo;
import com.kgc.smbms.pojo.SmbmsBill;
import com.kgc.smbms.pojo.SmbmsUser;
import com.kgc.smbms.service.SmbmsBillService;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * @ClassName SmbmsBillController
 * @Description TODO
 * @Author 86184
 * @Date 2021/8/23
 * @Version 1.0
 */
@Controller
@RequestMapping("/bill")
public class SmbmsBillController {
    @Autowired
    private SmbmsBillService smbmsBillService;
    /**
     * 当我们没有传递参数的时候，使用默认值
     *      取调存转
     * @param productname
     * @param providerid
     * @param ispayment
     * @param pageNo
     * @return
     */
    @RequestMapping("/list")
    public String list(@RequestParam(required = false,defaultValue = "") String productname,
                       @RequestParam(required = false,defaultValue = "0") Integer providerid,
                       @RequestParam(required = false,defaultValue = "0") Integer ispayment,
                       @RequestParam(required = false,defaultValue = "1") Integer pageNo,
                       Model model){
        //调
        PageInfo<SmbmsBill> pageInfo = smbmsBillService.selectBillByCondition(productname, providerid,
                ispayment, pageNo, 5);

        //存
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("productname",productname);
        model.addAttribute("providerid",providerid);
        model.addAttribute("ispayment",ispayment);

        //当数据查询出来之后，直接跳转到数据显示页面  转
        return "billlist";
    }
    @RequestMapping("/update")
    public String updateBill(SmbmsBill bill,HttpSession session,Model model){

        //获取用户对象
        SmbmsUser smbmsUser = (SmbmsUser) session.getAttribute("smbmsUser");
        //判断用户是否存在
        if(null == smbmsUser){
            model.addAttribute("error","请登录后再修改订单");
            return "forward:/login.jsp"; //转发到登录页面
        }

        //设置修改者id
        bill.setModifyby(smbmsUser.getId());
        //设置修改时间
        bill.setModifydate(new Date());

        //调
        int row = smbmsBillService.updateBill(bill);

        //如果修改失败
        if(row == 0){
            return "redirect:/bill/toUpdate?id="+bill.getId();
        }

        //如果修改成功，就跳转到主页
        return "redirect:/bill/list";
    }

    @RequestMapping("/toUpdate")
    public String toUpdate(Long id,Model model){
        //调
        SmbmsBill smbmsBill = smbmsBillService.selectBillById(id);
        //存
        model.addAttribute("bill",smbmsBill);
        //转
        return "billmodify";
    }

    @RequestMapping("/view")
    public String viewBill(Long id,Model model){

        //调
        SmbmsBill smbmsBill = smbmsBillService.selectBillById(id);

        //存
        model.addAttribute("bill",smbmsBill);
        //转
        return "billview"; //把结果查询到了之后需要跳转到详情页面
    }

    @RequestMapping("/delete")
    public String deleteBill(Long id){
        //返回值不用接收,接受和不接收没有区别
        smbmsBillService.deleteBill(id);
        return "redirect:/bill/list";
    }

    @RequestMapping("/add")
    public String addBill(SmbmsBill bill, HttpSession session,Model model){

        // System.out.println(bill);
        SmbmsUser smbmsUser = (SmbmsUser)session.getAttribute("smbmsUser");

        //判断用户是否登录?
        if(null == smbmsUser){ //如果用户没有登录,那么就让用户先去登录
            model.addAttribute("error","请登录后再添加订单");
            return "forward:/login.jsp";
        }

        //设置创建者？ 只需要拿到登录用户的主键值就可以了
        bill.setCreatedby(smbmsUser.getId());
        //设置创建时间
        bill.setCreationdate(new Date());

        //调用业务层的添加方法
        int row = smbmsBillService.addBill(bill);

        if(row == 0){ //表示添加失败
            //就继续回到添加页面,让用户继续添加
            return "redirect:/jsp/billadd.jsp";
        }

        //添加完成了之后要去哪里？
        return "redirect:/bill/list";
    }

}
